package com.wpen.unit.security;

import java.security.DigestException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * SHA1加密算法封装
 * 
 * @author Admin
 *
 */
public class SHA1 {
    private MessageDigest sha1; // sha1 object.

    public SHA1() throws NoSuchAlgorithmException {
        sha1 = MessageDigest.getInstance("SHA-1");
    }

    /**
     * get the cipher bytes length after sha1 operation. this value is fixed and
     * is 20 bytes.
     * 
     * @return the cipher bytes length.
     */
    public int getCipherLen() {
        return 20;
    }

    /**
     * reset the sha1 buffer to empty.
     */
    public void reset() {
        sha1.reset();
    }

    /**
     * update indata to sha1 buffer.
     * 
     * @param indata
     *            the input data.
     */
    public void update(byte[] indata) {
        sha1.update(indata);
    }

    /**
     * update indata to sha1 buffer.
     * 
     * @param indata
     *            the input data.
     * @param inoff
     *            the input data offset.
     * @param inlen
     *            the input data length.
     */
    public void update(byte[] indata, int inoff, int inlen) {
        sha1.update(indata, inoff, inlen);
    }

    /**
     * caculate the sha1 digest value from updated buffer data and reset the
     * buffer.
     * 
     * @param outdata
     *            the output data.
     * @param outoff
     *            the output data offset.
     * @throws DigestException
     */
    public int Final(byte[] outdata, int outoff) throws DigestException {
        return sha1.digest(outdata, outoff, 20);
    }

    /**
     * caculate the sha1 digest value from updated buffer data and reset the
     * buffer.
     * 
     * @return the output data.
     */
    public byte[] Final() {
        return sha1.digest();
    }

    /**
     * caculate the sha1 digest value from indata and reset the buffer.
     * 
     * @param indata
     *            the input data.
     * @param inoff
     *            the input data offset.
     * @param inlen
     *            the input data length.
     * @return the output data.
     */
    public byte[] digest(byte[] indata, int inoff, int inlen) {
        sha1.reset();
        sha1.update(indata, inoff, inlen);
        return sha1.digest();
    }

    /**
     * caculate the sha1 digest value from indata and reset the buffer.
     * 
     * @param indata
     *            the input data.
     * @param inoff
     *            the input data offset.
     * @param inlen
     *            the input data length.
     * @param outdata
     *            the output data.
     * @param outoff
     *            the output data offset.
     * @throws DigestException
     */
    public void digest(byte[] indata, int inoff, int inlen, byte[] outdata, int outoff) throws DigestException {
        sha1.reset();
        sha1.update(indata, inoff, inlen);
        sha1.digest(outdata, outoff, 20);
    }

    /**
     * caculate the sha1 digest value from indata and reset the buffer.
     * 
     * @param indata
     *            the input data.
     * @return the output data.
     */
    public byte[] digest(byte[] indata) {
        sha1.reset();
        sha1.update(indata);
        return sha1.digest();
    }
}
